#!/bin/bash  -e

function info() {
echo Usage: `basename $0` in.bam in.bed num
exit 65
}

while getopts  ":i:p:" opts
do
        case  $opts  in
        i) interval=$OPTARG;;
		p) out_prefix=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 3 ]; then info; fi

. /mnt/ilustre/app/medical/tools/.var


bam=`rel_abs.sh $1`

bed=`rel_abs.sh $2`

num=$3

dir=`mktemp -d bam.XXX`


cd $dir

bed-split.py $bed $num

for bed0 in $(ls); do

subq.sh -m4 -p4 -nsamseg "samtools view -b -L$bed0 -@4 $bam > $out_prefix.$bed0.bam"

done






cd -

. $cmd_done